import nodejs from '@dfeidao/atom-web/msg/nodejs';
import s_get from '@dfeidao/atom-web/storage/get';
import { IFeidaoAiBrowserComponent } from '@dfeidao/web/interfaces';

import render from '@dfeidao/atom-web/render/render';
import Tree from '@dfeidao/fd-w000001';

// 重新渲染树形图
import P01 from './P01';
import P04 from './P04';
export default async function a002(fd: IFeidaoAiBrowserComponent, args: Event) {


	const productid = await s_get('productid');


	const client_type = fd.data.node.querySelector<HTMLSelectElement>('[data-feidao-id="client_style"]').value;
	// fd.data._no = args[0];
	// set(fd, '_no', args[0]);
	// console.log(args[0])
	const formgroup_no = args;
	const res = await nodejs<Array<{ menu_no: string, menu_name: string, parent_no: string, _id: string, checked: boolean }>>('functional-permissions/zj-000002/s003', { productid, formgroup_no, client_type });

	// console.log(res, '00000')
	//
	if (res.length === 0) {
		render(fd, '', P01, 'P01', 'inner');
		render(fd, '', P04, 'P04', 'inner');
	} else {
		const cdata = res.map((d) => {
			return { id: d.menu_no, text: d.menu_name, p_id: d.parent_no, _id: d._id };
		});
		render(fd, JSON.stringify(cdata), P01, 'P01', 'inner');
		fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="P04"]').innerHTML = '';
	}

	const checked = new Array();
	res.forEach((item) => {
		if (item.checked === true) {
			checked.push(item.menu_no);
		}

	});
	// fd.data.checked = checked;
	const widget: Tree = document.querySelector('#widget');
	// console.log(checked, '6666')
	widget.check_node(checked);
}
